package com.sjtu.vfact.graph.view;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.sjtu.vfact.base.VfactEdge;
import com.sjtu.vfact.base.VfactVertex;
import com.sjtu.vfact.textual.model.Cluster;
import com.sjtu.vfact.textual.model.Document;
import com.sjtu.vfact.textual.model.MasterTermset;
import com.sjtu.vfact.textual.model.TermSet;

import edu.uci.ics.jung.graph.Graph;

import java.awt.Dimension;
import javax.swing.border.BevelBorder;
import javax.swing.JButton;
import javax.swing.border.CompoundBorder;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import java.awt.Color;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;


public class GraphFrame extends JFrame {

	private static final long serialVersionUID = 6437353701220993518L;
	
	private JPanel contentPane;
	private JPanel viewPanel;
	private GraphPanel graphPanel;
	
	private JButton btnCluster;
	private JButton btnUncluster;
	private JRadioButton rdBtnOn;
	private JRadioButton rdBtnOff;

	/**
	 * Create the frame.
	 */
	public GraphFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 855, 700);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		viewPanel = new JPanel();
		viewPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		contentPane.add(viewPanel, BorderLayout.CENTER);
		
		graphPanel = new GraphPanel();
		
		JPanel panelRight = new JPanel();
		contentPane.add(panelRight, BorderLayout.EAST);
		panelRight.setLayout(new BorderLayout(0, 0));
		
		JPanel editorPanel = new JPanel();
		editorPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		editorPanel.setPreferredSize(new Dimension(200, 10));
		panelRight.add(editorPanel, BorderLayout.CENTER);
		editorPanel.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new CompoundBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Clustering", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)), new BevelBorder(BevelBorder.LOWERED, null, null, null, null)));
		panel.setBounds(6, 6, 188, 100);
		editorPanel.add(panel);
		
		btnCluster = new JButton("cluster");
		panel.add(btnCluster);
		
		btnUncluster = new JButton("Uncluster");
		panel.add(btnUncluster);
		
		JPanel panelAnimation = new JPanel();
		panelAnimation.setBorder(new TitledBorder(null, "Animation", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelAnimation.setBounds(6, 105, 188, 59);
		editorPanel.add(panelAnimation);
		
		rdBtnOn = new JRadioButton("On");
		panelAnimation.add(rdBtnOn);
		
		rdBtnOff = new JRadioButton("Off");
		panelAnimation.add(rdBtnOff);
		
		ButtonGroup animationGroup = new ButtonGroup();
		animationGroup.add(rdBtnOn);
		animationGroup.add(rdBtnOff);
		
	}
	
	public void displayFrame(boolean state){
		setVisible(state);
	}
	
	public void generateGraph(List<MasterTermset> list){
		graphPanel.setupView(list);
		viewPanel.add(graphPanel.getGZSP());
	}
	
	public void generateSSAGraph(Map<TermSet, Set<Document>> ssaDict){
		graphPanel.setupSSAView(ssaDict);
		viewPanel.add(graphPanel.getGZSP());
	}
	
	public void clusterGraph(List<Cluster<VfactVertex>> clusters){
		graphPanel.clusterGraph(clusters);
	}
	
	public void unclusterGraph(){
		graphPanel.unclusterGraph();
	}
	
	public Graph<VfactVertex, VfactEdge> getGraph(){
		return graphPanel.getGraph();
	}
	
	public List<VfactVertex> getVertices(){
		return new ArrayList<VfactVertex>(graphPanel.getGraph().getVertices());
	}
	
	public void addBtnClusterListener(ActionListener l){
		btnCluster.addActionListener(l);
	}
	
	public void addBtnUnclusterListener(ActionListener l){
		btnUncluster.addActionListener(l);
	}
	
	public void addRdBtnAnimationOnListener(ActionListener l){
		rdBtnOn.addActionListener(l);
	}
	
	public void addRdBtnAnimationOffListener(ActionListener l){
		rdBtnOff.addActionListener(l);
	}
}
